package com.zhonghui.wms.controller;

import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.core.page.TableDataInfo;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.response.BaseResult;
import com.zhonghui.wms.domain.WmsReplenishmentApplication;
import com.zhonghui.wms.domain.vo.WmsReplenishmentApplicationVo;
import com.zhonghui.wms.service.IWmsReplenishmentApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 智能仓储WMS-补货申请Controller
 *
 * @author zhonghui
 * @date 2022-05-25
 */
@Api(tags="智能仓储WMS-补货申请")
@RestController
@RequestMapping("/wms/replenishmentApplication")
public class WmsReplenishmentApplicationController extends BaseController
{
    @Autowired
    private IWmsReplenishmentApplicationService wmsReplenishmentApplicationService;

    /**
     * 查询智能仓储WMS-补货申请列表
     */
    @ApiOperation("查询智能仓储WMS-补货申请列表")
    @GetMapping("/list")
    public TableDataInfo<List<WmsReplenishmentApplicationVo>> list(WmsReplenishmentApplication wmsReplenishmentApplication)
    {
        startPage();
        List<WmsReplenishmentApplicationVo> list = wmsReplenishmentApplicationService.selectWmsReplenishmentApplicationList(wmsReplenishmentApplication);
        return getDataTable(list);
    }

    /**
     * 导出智能仓储WMS-补货申请列表
     */
    @Log(title = "智能仓储WMS-补货申请", businessType = BusinessType.EXPORT)
    @ApiOperation("导出智能仓储WMS-补货申请列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsReplenishmentApplication wmsReplenishmentApplication)
    {
        List<WmsReplenishmentApplicationVo> list = wmsReplenishmentApplicationService.selectWmsReplenishmentApplicationList(wmsReplenishmentApplication);
        ExcelUtil<WmsReplenishmentApplicationVo> util = new ExcelUtil<WmsReplenishmentApplicationVo>(WmsReplenishmentApplicationVo.class);
        util.exportExcel(response, list, "智能仓储WMS-补货申请数据");
    }

    /**
     * 获取智能仓储WMS-补货申请详细信息
     */
    @ApiOperation("获取智能仓储WMS-补货申请详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<WmsReplenishmentApplication> getInfo(@PathVariable("id") Long id)
    {
        return BaseResult.success(wmsReplenishmentApplicationService.selectWmsReplenishmentApplicationById(id));
    }

    /**
     * 新增智能仓储WMS-补货申请
     */
    @ApiOperation("新增智能仓储WMS-补货申请")
    @Log(title = "智能仓储WMS-补货申请", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult<Integer> add(@RequestBody WmsReplenishmentApplication wmsReplenishmentApplication)
    {
        // 申请状态 0未提交 1待审核 2已审核
        wmsReplenishmentApplication.setApplicationStatus(Long.parseLong("0"));
        return BaseResult.success(wmsReplenishmentApplicationService.insertWmsReplenishmentApplication(wmsReplenishmentApplication));
    }

    /**
     * 修改智能仓储WMS-补货申请
     */
    @ApiOperation("修改智能仓储WMS-补货申请")
    @Log(title = "智能仓储WMS-补货申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody WmsReplenishmentApplication wmsReplenishmentApplication)
    {
        wmsReplenishmentApplication.setApplicationStatus(Long.parseLong("0"));
        wmsReplenishmentApplication.setApproved(null);
        wmsReplenishmentApplication.setApprovedStatus(null);
        wmsReplenishmentApplication.setApprovedDate(null);
        wmsReplenishmentApplication.setApprovedComments("");
        return BaseResult.success(wmsReplenishmentApplicationService.updateWmsReplenishmentApplication(wmsReplenishmentApplication));
    }

    /**
     * 提交智能仓储WMS-补货申请
     */
    @ApiOperation("提交智能仓储WMS-补货申请")
    @Log(title = "补货申请", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public BaseResult<Integer> submit(@RequestBody WmsReplenishmentApplication wmsReplenishmentApplication) {
        wmsReplenishmentApplication.setApplicationStatus(Long.parseLong("1"));
        wmsReplenishmentApplication.setApplicationDate(DateUtils.getNowDate());
        if(wmsReplenishmentApplication.getId() != null) {
            // 修改提交
            WmsReplenishmentApplication applicationVo = wmsReplenishmentApplicationService.selectWmsReplenishmentApplicationById(wmsReplenishmentApplication.getId());
            if(applicationVo == null) {
                return BaseResult.error("此入库申请不存在，不能提交！");
            }
            wmsReplenishmentApplication.setApproved(null);
            wmsReplenishmentApplication.setApprovedStatus(null);
            wmsReplenishmentApplication.setApprovedDate(null);
            wmsReplenishmentApplication.setApprovedComments("");
            return BaseResult.success(wmsReplenishmentApplicationService.updateWmsReplenishmentApplication(wmsReplenishmentApplication));
        }else {
            // 申请状态 0未提交 1待审核 2已审核
            wmsReplenishmentApplication.setApplicationStatus(Long.parseLong("1"));
            // 新增提交
            return BaseResult.success(wmsReplenishmentApplicationService.insertWmsReplenishmentApplication(wmsReplenishmentApplication));
        }
    }

    /**
     * 审核智能仓储WMS-补货申请
     */
    @ApiOperation("审核智能仓储WMS-补货申请")
    @Log(title = "补货申请", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public BaseResult<Integer> approve(@RequestBody WmsReplenishmentApplication wmsReplenishmentApplication) {
        wmsReplenishmentApplication.setApplicationStatus(Long.parseLong("2"));
        wmsReplenishmentApplication.setApprovedDate(DateUtils.getNowDate());
        wmsReplenishmentApplication.setApproved(SecurityUtils.getLoginUser().getUser().getUserId());
        return BaseResult.success(wmsReplenishmentApplicationService.updateWmsReplenishmentApplication(wmsReplenishmentApplication));
    }

    /**
     * 删除智能仓储WMS-补货申请
     */
    @ApiOperation("删除智能仓储WMS-补货申请")
    @Log(title = "智能仓储WMS-补货申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        return BaseResult.success(wmsReplenishmentApplicationService.deleteWmsReplenishmentApplicationByIds(ids));
    }

    @ApiOperation("智能仓储WMS-补货申请-生成采购单")
    @Log(title = "生成采购单", businessType = BusinessType.INSERT)
    @PostMapping("/genPurchaseApply")
    public BaseResult<Integer> genPurchaseApply(@RequestBody WmsReplenishmentApplication wmsReplenishmentApplication) {
        return BaseResult.success(wmsReplenishmentApplicationService.genPurchaseApply(wmsReplenishmentApplication.getId()));
    }
}
